import unittest

from qcpu import QCpuMemory

class Test_qcpu(unittest.TestCase):
    def test_qcpumemory(self):
        mem = QCpuMemory()
        
        # read/write byte
        self.assertEqual(mem[0], 0)
        mem[0] = 1
        mem[512:514] = [2, 3]
        self.assertEqual(mem[0], 1)
        self.assertEqual(mem[512], 2)
        
        # get device
        self.assertEqual(mem.get(0x9C, 0, 2)[0], 1)
        self.assertEqual(mem.get(0x9D, 0, 2)[1], 3)
        
        # set device
        mem.set(0x9C, 0, [10, 11])
        mem.set(0x9D, 0, "\x20\x00\x21\x00")
        self.assertEqual(mem[1],   11)
        self.assertEqual(mem[513], 0x21)
        
        # save/load
        mem.save("memory.dat")
        del mem
        mem = QCpuMemory()
        self.assertEqual(mem[1], 0)
        mem.load("memory.dat")
        self.assertEqual(mem[1], 11)
        
        # bit
        self.assertEqual(mem.getbit([(0x9C, 0)]), 0)
        self.assertEqual(mem.getbit([(0x9C, 1)]), 1)
        mem.setbit([(0x9C, 0)], 1)
        mem.setbit([(0x9C, 1)], 0)
        self.assertEqual(mem[0], 9)
        self.assertEqual(mem.getbit([(0x9C, 0)]), 1)
        self.assertEqual(mem.getbit([(0x9C, 1)]), 0)
        
        # bits
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 0)]), 9)
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 16)]), 11)
        mem.setbits([(0xF1, 1), (0x9C, 14)], 3)
        self.assertEqual(mem.getbits([(0xF1, 1), (0x9C, 14)]), 3)
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 0)]), 0xc009)
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 16)]), 8)
        mem.setbits([(0xF1, 5), (0x9C, 31)], 0xF0003)
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 16)]), 0x8008)
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 32)]), 0x8001)
        self.assertEqual(mem.getbits([(0xF1, 4), (0x9C, 48)]), 7)
        
        # long
        #
        
        #mem.SM[400] = True
        
if __name__ == '__main__':
    unittest.main()    